package org.pratik.ghine;
import ClassesInfo.configuration;

import java.io.IOException;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

///import website.web.service.LoginService;

/**
 * Servlet implementation class WelcomeServlet
 */
@WebServlet(description = "When the user logins", urlPatterns = { "/WelcomeServletPath" })
public class WelcomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RequestDispatcher formJsp,indexDispatcher;
	private configuration conf;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Welcome servlet");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		String username=request.getParameter("username");
		out.println("hii get "+username);
	}
	@Override
		public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		ServletContext context = config.getServletContext();
		//formJsp = context.getRequestDispatcher("/form.jsp");
		formJsp = context.getRequestDispatcher("/create_topic.jsp");
		indexDispatcher=context.getRequestDispatcher("/index.html");
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Welcome servlet");
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		String username=request.getParameter("username");
		out.println("hii from post "+username);
		String userName = request.getParameter("username");
        String driver="com.mysql.jdbc.Driver";
        String userPass = request.getParameter("password");
        Statement st=null;
        ResultSet rs=null;
        conf=new configuration();
       
        boolean flag=false;
		try {
			 //login=new LoginService();
				try {
					//System.out.println("Inside try");
					Class.forName(driver).newInstance();
					
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					System.out.println("Class not founddddddddddddddddd  "+e.getMessage());
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
					
					Connection con = null;
					int count=0;
				
					
					try {
						//System.out.println("jaj");
						//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ghine","root","25955103");
						con=conf.getConnection();
						if(con==null)
						{
							out.println("aa");
							
						}
													
							
							
					} catch (SQLException e1) {
						System.out.println("coneectionn  "+e1.getMessage());
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					st=con.createStatement();
					
					//PreparedStatement  statement=con.prepareStatement("select * from login");// where name"//='"+userName+"' and password='"+passWord+"'");
					//ResultSet result=statement.executeQuery();
					rs=st.executeQuery("select * from login where name='"+username+"' and password='"+userPass+"'");
					

					while(rs.next())
					{
						count++;
						System.out.println(rs.getString(1)+" "+rs.getString(3));
						
					}
					if(count>0)
					{
						flag=true;
						
						out.println("Aaaaaaaaaaa");
						
					}
					else flag=false;
					con.close();
			//result = login.authenticate(userName, userPass);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        if(flag)
        {
       	 out.println("count greater!!");
       	 //request.setAttribute("message", "bye");
 		 formJsp.forward(request, response);
       	 
          	 return;
        }
        else
        {
        	
        	out.println("count less than ");
        	indexDispatcher.forward(request, response);
       	 //response.sendRedirect("/website/home");
        }
        
	}

}
